﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Player : MonoBehaviour
{

    //战机移动速度
    float player_Speed = 3;
    //发射子弹间隔
    protected float m_Timer = 0;
    //时间
    public float m_Timer2 = 0.2f;
    //子弹发射音效
    public AudioClip rocket1_Sounds;
	public LayerMask m_layer;
    protected AudioSource m_audio;
    //生命
    public int player_Life = 5;
	//是否减少生命
	public static bool player_Life_bool = true;
    //子弹等级
    int zidan_DengJi = 0;
    //是否检测防护罩
    bool fanghuzhao_bool = false;
	GameObject fanghuzhao;
	//防护罩抵挡的伤害
	int fanghuzhao_Life =5;
    //是否实例化翅膀
    bool chibang_bool = true;
    //记录手指触摸位置
    Vector2 m_screenpos = new Vector2();
    Transform m_Transform;

    void Start()
    {
        m_Transform = this.transform;
        m_audio = GetComponent<AudioSource>();
        //允许多点触控
        Input.multiTouchEnabled = true;
    }

    void Update()
    {

        
        //发射子弹
        if (m_Timer <= 0)
        {
            this.Firedbullets(zidan_DengJi);
        }
        m_Timer -= Time.deltaTime;
        if (Input.GetKeyUp(KeyCode.Space))
        {
            m_Timer = 0;
        }

        //飞机血量显示
        Image obj = GameObject.Find("XueLiang").gameObject.GetComponent<Image>();
        obj.fillAmount = 0.2f * player_Life;

		//手指控制移动
		if(Input.GetMouseButton(0))
		{
		   this.chuMo2();
		}
        this.dianNaoControl();
	}

    //电脑操作
    void dianNaoControl()
    {
        //战机横向移动距离
        float m_X = 0;
        //战机纵向移动距离
        float m_Y = 0;

        if (Input.GetKey(KeyCode.A))
        {
            if (m_Transform.position.x >= -2.7f)
            {
                m_X -= player_Speed * Time.deltaTime;
            }
        }
        if (Input.GetKey(KeyCode.D))
        {
            if (m_Transform.position.x <= 2.7f)
            {
                m_X += player_Speed * Time.deltaTime;
            }
        }
        if (Input.GetKey(KeyCode.W))
        {
            if (m_Transform.position.y <= 6.1f)
            {
                m_Y += player_Speed * Time.deltaTime;
            }
        }
        if (Input.GetKey(KeyCode.S))
        {
            if (m_Transform.position.y >= -4.2f)
            {
                m_Y -= player_Speed * Time.deltaTime;
            }
        }
        m_Transform.Translate(m_X, m_Y, 0);
    }
	//触摸
	void chuMo()
	{
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;
		if(Physics.Raycast(ray,out hit))
		{
			if(hit.transform.tag.CompareTo("Plane") == 0)
			{
				m_Transform.position = Vector3.MoveTowards(m_Transform.position,hit.point,0.3f);
			}
		}

	}
    //触摸2
    void chuMo2()
    {
        if (Input.touchCount == 1)
        {
            //开始触摸
            if (Input.touches[0].phase == TouchPhase.Began)
            {
                //记录手指触摸的位置
                m_screenpos = Input.touches[0].position;
            }
            else if (Input.touches[0].phase == TouchPhase.Moved)
            {
                m_Transform.Translate(new Vector3(Input.touches[0].deltaPosition.x * Time.deltaTime, Input.touches[0].deltaPosition.y *1.3F* Time.deltaTime,0));
            }
        }
    }

    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag.CompareTo("Enemy") == 0 || other.tag.CompareTo("Enemy_Rocket") == 0)
        {	
			if(player_Life_bool)
			{
                Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[18].ToString()));
                this.Life(2);
			}
        }
        else if (other.tag.CompareTo("Enemy2") == 0)
        {
			if(player_Life_bool)
			{
                Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[18].ToString()));
                this.Life(2);
			}
        }
        else if (other.tag.CompareTo("Rocket_Shengji") == 0)
        {
            //升级子弹等级
            zidan_DengJi += 1;
			if(zidan_DengJi>=2)
			{
				zidan_DengJi =2;
			}
        }
        else if (other.tag.CompareTo("ChiFangHuZhao") == 0)
        {
			fanghuzhao=GameObject.FindGameObjectWithTag("FangHuZhao");
			if(fanghuzhao==null)
			{
				Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[7].ToString()));
			}
            FangHuZhao.fanghuzhao_Life = 5;
			player_Life_bool = false;
        }
        else if (other.tag.CompareTo("ChiChiBang") == 0)
        {
			GameObject obj2=GameObject.FindGameObjectWithTag("JiYiTeXiao-1");
			if(obj2==null)
			{
				Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[8].ToString()));
			}
        }else if(other.tag.CompareTo("xiao_BOSS1") == 0||player_Life <= 0)
		{
			this.Dead();
        }
    }

    public void Life(int m_life)
    {
        if (player_Life_bool)
        {
            player_Life -= m_life;
            if (m_life <= 0)
            {
                this.Dead();
            }
        }
    }

    //子弹发射
    protected void Firedbullets(int m_a)
    {
        Vector3 player_Pos = m_Transform.position;
		Quaternion rotation = Quaternion.identity;
		rotation.eulerAngles = new Vector3(0,0,180);
        switch (m_a)
        {
            case 0:
			    Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[26].ToString()),new Vector3(player_Pos.x,player_Pos.y+0.6f,0),rotation);
			    m_audio.PlayOneShot(rocket1_Sounds);
                m_Timer = m_Timer2;
                break;
            case 1:      
			Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[26].ToString()),new Vector3(player_Pos.x,player_Pos.y+0.6f,0),rotation);
			Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[27].ToString()),new Vector3(player_Pos.x,player_Pos.y+0.5F,0),rotation);
                m_audio.PlayOneShot(rocket1_Sounds);
                m_Timer = m_Timer2;
                break;
		    case 2:
			    Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[26].ToString()),new Vector3(player_Pos.x,player_Pos.y+0.6f,0),rotation);
			Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[27].ToString()),new Vector3(player_Pos.x,player_Pos.y+0.5F,0),rotation);
			    Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[28].ToString()),m_Transform.position,m_Transform.rotation);
			    m_audio.PlayOneShot(rocket1_Sounds);
			    m_Timer = m_Timer2;
			    break;
        }
    }

	//死亡特效
	private void Dead()
	{
		Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[11].ToString()),m_Transform.position,m_Transform.rotation);
		Destroy(this.gameObject,0.2f);
		Application.LoadLevel("Menu");
	}
}
